#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
try:
    import ujson as json
except:
    import json
from utils import log
from controls import mongo
from controls import data_topic_title_mysql
from utils.file_util import file_upload


class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_secure_cookie("username")

class AddTopicHandler(BaseHandler):
    def get(self, ):
        # if not self.current_user:
        #     self.redirect("/cms/signin")
        #     return
        self.render('cms2/topic.html')
        return

    def post(self,):
        provid = self.get_argument('provid')
        title = self.get_argument('title')
        summary = self.get_argument('summary')

        if not (provid and title and summary):
            res = {'ret': 1, 'msg': 'param error'}
            self.write(json.dumps(res))
            self.finish()
            return

        topicid = mongo.count_topicid()

        file = self.request.files['file'][0]
        file_res = file_upload(file)

        if not (file_res and file_res['ret'] == 0):
            res = {'ret': 1, 'msg': '保存图片失败'}
            self.write(json.dumps(res))
            self.finish()
            return

        new_item = {
            'topicid': topicid,
            'title': title,
            'summary': summary,
            'hot': 0,
            'cityid': 0,
            'provid': int(provid),
            'imgid': file_res['res']['imgid'],
        }

        if not data_topic_title_mysql.add_topic_title_item(new_item):
            res = {'ret': 1, 'msg': 'add error'}
            self.write(json.dumps(res))
            self.finish()
            return

        res = {'ret': 0, 'msg': '保存成功'}
        self.write(json.dumps(res))
        self.finish()
        return

class UpdateTopicHandler(BaseHandler):
    def post(self, *args, **kwargs):
        topicid = self.get_argument('topicid')
        title = self.get_argument('title')
        summary = self.get_argument('summary')
        provid = self.get_argument('provid')
        cityid = self.get_argument('cityid')

        if not (topicid and title and summary and provid and cityid):
            res = {'ret': 1, 'msg': 'param error'}
            self.write(json.dumps(res))
            self.finish()
            return

        file = self.request.files['file'][0]
        file_res = file_upload(file)

        if not (file_res and file_res['ret'] == 0):
            res = {'ret': 1, 'msg': '保存图片失败'}
            self.write(json.dumps(res))
            self.finish()
            return

        item = {
            'topicid': topicid,
            'title': title,
            'summary': summary,
            'hot': 0,
            'cityid': int(cityid),
            'provid': int(provid),
            'imgid': file_res['res']['imgid'],
        }

        if not data_topic_title_mysql.update_topic_title_by_topicid(item):
            res = {'ret': 1, 'msg': 'add error'}
            self.write(json.dumps(res))
            self.finish()
            return

        res = {'ret': 0, 'msg': '保存成功'}
        self.write(json.dumps(res))
        self.finish()
        return


class ApiNotFindHandler( BaseHandler ) :

    def get( self ):
        raise tornado.web.HTTPError( 503 )
    @tornado.web.asynchronous
    def post( self ):
        log.i( self  )
        res = { 'idx':0, 'ret':9, 'msg':'无此功能', 'res':{} }
        self.write( json.dumps(res) )
        self.finish()
